﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml.Serialization;

namespace test12
{



public class Client
    {
        public Client() { }
        public string Cname
        {
            get;
            set;
        }
        public string work
        {
            get;
            set;
        }
        public string ToString()
        {
            return string.Format("客户的名称是:" + Cname + ",职业是：" + work);
        }
        public Client(string a, string b)
        {
            Cname = a;
            work = b;
        }
    }
    public class goods
    {
        public goods() { }
        public string gname
        {
            get;
            set;
        }
        public int gprice
        {
            get;
            set;
        }
        public string ToString()
        {
            return string.Format("商品的名称为：" + gname);
        }
        public goods(string a, int b)
        {
            gname = a;
            gprice = b;
        }
    }



    public class Order : IEquatable<Order>
    {
        public int orderNumber
        {
            get;
            set;
        }
        public String goodsName
        {
            get;
            set;
        }
        public String clientName
        {
            get;
            set;
        }

        public String clientWork
        {
            get;
            set;
        }


        public int moneyNum
        {
            get;
            set;
        }
        //override
        public override bool Equals(object? obj)
        {
            return this.Equals(obj as Order);
        }
        public bool Equals(Order other)
        {
            //Order other = (Order)obj;
            if (other == null) return false;
            if (this.orderNumber != other.orderNumber) return false;
            //if (this.moneyNum != other.orderNumber) return false;
            //if (!this.client .Equals(other.client)) return false;
            //if (!this.goodsName.Equals(other.goodsName)) return false;
            return true;
        }
        public override int GetHashCode()
        {
            return this.orderNumber.GetHashCode();
        }
        public string ToString()
        {
            return string.Format("订单号为：" + orderNumber + "," + goodsName.ToString() + "," + clientName + ",订单价格为" + moneyNum);
        }
        public Order(int a, String b, String c,String work, int d)
        {
            orderNumber = a;
            goodsName = b;
            clientName = c;
            clientWork = work;
            moneyNum = d;
        }
        public Order() { }
    }
    class OrderDetails
    {

    }
    public class OrderService
    {
        public List<Order> data;
        private HashSet<Order> set;
        public OrderService()
        {
            data = new List<Order>();
            set = new HashSet<Order>();
        }
        public void AddOrder(Order order)
        {
            if (set.Contains(order))
            {
                Console.WriteLine("订单号为" + order.orderNumber + "的订单已存在，请不要重复添加");
                Console.WriteLine();
                return;
            }
            this.data.Add(order);
            set.Add(order);
        }
        public void DeleteOrder(int num)
        {
            for (int i = 0; i < this.data.Count(); i++)
            {
                if (this.data[i].orderNumber.Equals(num))
                {
                    Order o = data[i];
                    this.data.Remove(o);
                    return;
                }
            }

            Console.WriteLine();
        }
        public void UpdateOrder(Order order)
        {
            for (int i = 0; i < this.data.Count(); i++)
            {
                if (this.data[i].Equals(order))
                {
                    this.data[i].clientName = order.clientName;
                    this.data[i].clientWork = order.clientWork;
                    this.data[i].goodsName = order.goodsName;
                    this.data[i].moneyNum = order.moneyNum;
                    Console.WriteLine("已更改订单号为" + order.orderNumber + "的订单");
                    return;
                }
            }
            Console.WriteLine("不存在订单号为" + order.orderNumber + "的订单，无法修改");
            Console.WriteLine();
        }
        public List<Order> SearchOrderByOrderNum(int num)
        {
            Console.WriteLine("订单号为" + num + "的订单如下:");
            var m = from n in data where n.orderNumber == num orderby n.moneyNum select n;
            foreach (var n in m)
            {
              
                Console.WriteLine(n.ToString());
            }
            if (m.Count() == 0) Console.WriteLine("订单号为" + num + "的订单不存在");
            Console.WriteLine();
            return m.ToList();
            

        }

       
        public void SearchOrderByrMoneyNum(int num)
        {
            Console.WriteLine("价格为" + num + "的订单如下:");
            var m = from n in data where n.moneyNum == num orderby n.moneyNum select n;
            foreach (var n in m)
            {
                Console.WriteLine(n.ToString());
            }
            if (m.Count() == 0) Console.WriteLine("价格为" + num + "的订单不存在");
            Console.WriteLine();

        }

        public void sort()//希尔排序
        {
            int i, j;
            int n = data.Count();
            int d = data.Count() / 2;

            while (d > 0)
            {

                for (i = d; i < n; i++)
                {

                    j = i - d;
                    Order temp = data[i];
                    while (j >= 0 && data[j].orderNumber > temp.orderNumber)
                    {
                        data[j + d] = data[j];
                        j -= d;
                    }
                    data[j + d] = temp;
                }
                d /= 2;

            }
            Console.WriteLine("订单排序之后的结果为");
            for (int ii = 0; ii < data.Count; ii++)
            {

                Console.WriteLine(data[ii].ToString());
            }
            Console.WriteLine();
        }

        public void Export()
        {
            XmlSerializer xml = new XmlSerializer(typeof(List<Order>));
            using (FileStream fs = new FileStream("o.xml", FileMode.Create))
            {
                xml.Serialize(fs, data);
            }
            Console.WriteLine(File.ReadAllText("o.xml"));
        }
        public void Import(string s)
        {
            XmlSerializer xml = new XmlSerializer(typeof(List<Order>));
            using (FileStream fs = new FileStream(s, FileMode.Open))
            {
                this.data = (List<Order>)xml.Deserialize(fs);
            }
        }
    }


}
